package com.aiyige.page.my.dynamicmanagement.adapter;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.db.table.InfoModel;
import com.aiyige.base.db.table.LearnVideoModel;
import com.aiyige.base.db.table.MajorCourseDBModel;
import com.aiyige.base.db.table.NormalVideoModel;
import com.aiyige.base.db.table.PhotoModel;
import com.aiyige.base.db.table.SpecialTopicDBModel;
import com.aiyige.base.db.table.TraingCardDBModel;
import com.aiyige.base.db.table.TraingClassDBModel;
import com.aiyige.page.detail.DetailPage;
import com.aiyige.page.my.dynamicmanagement.model.SearchResultItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/11/21.
 */

public class DMSearchResultAdapter extends BaseQuickAdapter<SearchResultItem, BaseViewHolder> {
    FragmentActivity mActivity;

    public DMSearchResultAdapter(FragmentActivity activity) {
        super(0, new LinkedList<SearchResultItem>());
        mActivity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchResultItem item) {
        switch (item.getItemType()) {
            case PublishUtil.PARENT_TYPE_INFO:
                ((InfoViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_PHOTO:
                ((PhotoViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                ((NormalVideoViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                ((SpecialTopicViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getItemType();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                baseViewHolder = new SpecialTopicViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_INFO:
                baseViewHolder = new InfoViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_PHOTO:
                baseViewHolder = new PhotoViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                baseViewHolder = new NormalVideoViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            default:
                baseViewHolder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewHolder;
    }

    public class InfoViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        DMAdapter dmAdapter;


        public InfoViewHolder(View view) {
            super(view);
            dmAdapter = new DMAdapter(mActivity);
            dmAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            dmAdapter.bindToRecyclerView(recyclerView);
            dmAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    InfoModel item = (InfoModel) dmAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalInfo() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.info) + "(" + data.getTotalInfo() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.info));
            }
            if (ListUtil.isEmpty(data.getInfoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                dmAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalInfo() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getInfoModelList().subList(0, Math.min(data.getInfoModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            dmAdapter.setNewData(destList);
        }
    }

    public class SpecialTopicViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        DMAdapter dmAdapter;


        public SpecialTopicViewHolder(View view) {
            super(view);
            dmAdapter = new DMAdapter(mActivity);
            dmAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            dmAdapter.bindToRecyclerView(recyclerView);
            dmAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    SpecialTopicDBModel item = (SpecialTopicDBModel) dmAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalSpecialTopic() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.special_topic) + "(" + data.getTotalSpecialTopic() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.special_topic));
            }
            if (ListUtil.isEmpty(data.getSpecialTopicDBModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                dmAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalSpecialTopic() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getSpecialTopicDBModelList().subList(0, Math.min(data.getSpecialTopicDBModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            dmAdapter.setNewData(destList);
        }
    }


    public class NormalVideoViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        DMAdapter dmAdapter;


        public NormalVideoViewHolder(View view) {
            super(view);
            dmAdapter = new DMAdapter(mActivity);
            dmAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            dmAdapter.bindToRecyclerView(recyclerView);
            dmAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    NormalVideoModel item = (NormalVideoModel) dmAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalNormalVideo() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.video) + "(" + data.getTotalNormalVideo() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.video));
            }
            if (ListUtil.isEmpty(data.getNormalVideoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                dmAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalNormalVideo() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getNormalVideoModelList().subList(0, Math.min(data.getNormalVideoModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            dmAdapter.setNewData(destList);
        }
    }

    public class PhotoViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        DMAdapter dmAdapter;


        public PhotoViewHolder(View view) {
            super(view);
            dmAdapter = new DMAdapter(mActivity);
            dmAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            dmAdapter.bindToRecyclerView(recyclerView);
            dmAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    PhotoModel item = (PhotoModel) dmAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalPhoto() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.photo) + "(" + data.getTotalPhoto() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.photo));
            }
            if (ListUtil.isEmpty(data.getPhotoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                dmAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalPhoto() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getPhotoModelList().subList(0, Math.min(data.getPhotoModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            dmAdapter.setNewData(destList);
        }
    }

}
